package com.vq3.digitalizationsoftwareintegrates.Configs;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.vq3.digitalizationsoftwareintegrates.Utils.GetYAML;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

/**注册中心
 * @author y.h
 * @version 1.0.0
 *
 * */

@ComponentScan
@Configuration
class InterceptorConfig extends WebMvcConfigurationSupport {
    @Resource
    GetYAML getYAML;

    @Override
    protected void addInterceptors(@NotNull InterceptorRegistry registry) {

        super.addInterceptors(registry);
    }
/**
 * 注册静态资源
 * */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        // TODO: 2022/12/11  添加了绝对路径，部署的时候记得更换
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        registry.addResourceHandler("/Spot_check/TempImage/**").addResourceLocations("file:"+getYAML.getTempImgPath());
        registry.addResourceHandler("/Spot_check/SysImg/**").addResourceLocations("file:"+getYAML.getSysImgPath());
        registry.addResourceHandler("/Spot_check/SCEXImg/**").addResourceLocations("file:"+getYAML.getSCEXImgPath());
        registry.addResourceHandler("/Canva.png").addResourceLocations("classpath:/static/Img/PageImg/Canva.png");
        super.addResourceHandlers(registry);
    }
    /**
     * 注册静态页面
     * */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/login");
        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/Spot_Check.html").setViewName("Spot_Check/Spot_Check");

        super.addViewControllers(registry);



    }

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST","OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("Access-Control-Allow-Headers",
                        "Access-Control-Allow-Methods",
                        "Access-Control-Allow-Origin",
                        "Access-Control-Max-Age",
                        "X-Frame-Options")
                .allowCredentials(false)
                .maxAge(3600);
        super.addCorsMappings(registry);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //1 创建MybatisPlusInterceptor拦截器对象
        MybatisPlusInterceptor mpInterceptor=new MybatisPlusInterceptor();
        //2 添加分页拦截器
        mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mpInterceptor;
    }
}
